library(ggplot2)
library(readr)
library(broom)
library(broom.mixed)
library(estimatr)
library(modelsummary)
library(tidyverse)

df <- read_csv("sesdata.csv")
df <- df %>% 
  mutate(gender = as.factor(gender2),
         DVcat = as.factor(gra2X),
         treat = as.factor(treat2),
         treatdummy = as.factor(treatdum),
         treatnumeric = as.numeric(treat2),
         lgb = as.factor(lgb2),
         trans = as.factor(trans2),
         queer = as.factor(queer),
         gengay = as.factor(gengay),
         degree = as.factor(degree2),
         nonwhite = as.factor(nonwhite),
         urban = as.factor(urban2),
         interest = as.factor(interest2),
         partyID = as.factor(vote2),
         age = as.numeric(w2age),
         outcome = as.numeric(gra2X)
  )

womenonly <- subset(df, gender == 1)
menonly <- subset(df, gender == 0)

##Main treatment effects##
model1 <-lm (outcome ~ treat, data=df, weight=weight)
summ(model1)
model2<-lm (outcome ~ treatdummy, data=df, weight=weight)

##Main treatment by gender subsamples##
model3 <-lm (outcome ~ treat, data=womenonly, weight=weight)
model4 <-lm (outcome ~ treat, data=menonly, weight=weight)

###Observational models###
models2 <- list()
models2 [['Model 1']] <-lm (outcome ~ gender + age + lgb + trans + incomenew + degree + nonwhite + urban, data=df, weight=weight)
models2 [['Model 2']] <-lm (outcome ~ gender + age + lgb + trans + incomenew + degree + nonwhite + urban + RILE + GALTAN + partyID, data=df, weight=weight)
msummary(models2, star=TRUE, output='latex')

##Interaction models
modelsX <- list()
modelsX [['Model 1']] <-lm (outcome ~ treat, data=df, weight=weight)
modelsX [['Model 2']] <-lm (outcome ~ treat*RILE + gender + age + lgb + trans + incomenew + degree + nonwhite + urban + GALTAN + partyID, data=df, weight=weight)
modelsX [['Model 3']] <-lm (outcome ~ treat*GALTAN + gender + age + lgb + trans + incomenew + degree + nonwhite + urban + RILE + partyID, data=df, weight=weight)
msummary(modelsX, star=TRUE, output='latex')
